import Common from '../../common/common.js';
let common = new Common;
const app = getApp()
Page({ 
  data: {
    noticeList:[ ], 
    pageNumber: 1,
    isEnd: false, //是否到底
  },
  onLoad: function (options) {
 
  },  
  onShow: function () {
    this.queryUserMessage(1,1);
  },  
  // 获取消息通知列表
  queryUserMessage(page, type){
    let data = {
      "size": 10,
      "current": page, 
      "condition": {
        'status':1 ,
        'orgSeq':app.globalData.orgSeq
      }
    }
    common.requestAjax('/api/user-service/v1/userMessage/queryMessageByOrgSeq','POST',data).then(res=>{
      if (res.data.code) {
        let arraylist = this.data.noticeList || [];
        let isEnd = this.data.isEnd;
        if (type == 1) {
          //下拉刷新
          arraylist = res.data.records;
          isEnd = false;
        } else if (type == 2) {
          //上拉加载
          arraylist = arraylist.concat(res.data.records);
          if (res.data.records.length == 0) {
            //最后一行没数据 到底了
            isEnd = true;
          }
          setTimeout(() => {
            wx.hideLoading()
          }, 500)
        }
        arraylist.forEach(item=>{
          item.messageContentList = item.messageContent.split('\\n');
        })
        this.setData({
          noticeList: arraylist,
          isEnd,
        })
      }
    })
  },
  onReachBottom: function () {
    if (!this.data.isEnd) {
      let pageNumber = this.data.pageNumber;
      pageNumber += 1;

      this.queryUserMessage(pageNumber, 2)

      wx.showLoading({
        title: '正在加载中...',
      })
      this.setData({
        pageNumber
      })
    }
  }, 
  // 消息详情
  inforDetail(e){ 
    console.log(this.data.pageNumber)
    let id = e.currentTarget.dataset.id 
    let data = { 
      messageId: id, 
      orgSeq:app.globalData.orgSeq,
    }
    common.requestAjax('/api/user-service/v1/userMessage/addUserMessageInfo','POST',data).then(res=>{
      if (res.data.code == 1) { 
        wx.navigateTo({
          url: '/pages/infor/inforDetail/index?id='+id+"&pageNumber="+this.data.pageNumber,
        })
      }else{
        common.showToast(res.data.message);
      }
    })
    },
})